package br.edu.ufcg.msnlab2.grupo05.Grafico;

import java.awt.geom.Rectangle2D;
import java.util.ArrayList;

import javax.swing.JPanel;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.YIntervalSeries;
import org.jfree.data.xy.YIntervalSeriesCollection;

import br.edu.ufcg.msnlab2.misc.Point;

public class Graph {
	
	private ArrayList<Point> points;
	
	public Graph(ArrayList<Point> points){
		this.points = points;
	}
	
	private JFreeChart createChart(XYDataset paramXYDataset){
		JFreeChart localJFreeChart = ChartFactory.createXYLineChart("", "x", "y", paramXYDataset, PlotOrientation.VERTICAL, true, true, false);
	    XYPlot localXYPlot = (XYPlot)localJFreeChart.getPlot();
	    localXYPlot.setDomainZeroBaselineVisible(true);
	    localXYPlot.setRangeZeroBaselineVisible(true);
	    localXYPlot.getDomainAxis().setLowerMargin(0.0D);
	    localXYPlot.getDomainAxis().setUpperMargin(0.0D);
	    localXYPlot.setDomainPannable(true);
	    localXYPlot.setRangePannable(true);
	    XYLineAndShapeRenderer localXYLineAndShapeRenderer = (XYLineAndShapeRenderer)localXYPlot.getRenderer();
	    localXYLineAndShapeRenderer.setLegendLine(new Rectangle2D.Double(-4.0D, -3.0D, 8.0D, 6.0D));
	    
	    /*
	    //set y axis to integer unit.
	    NumberAxis localNumberAxis = (NumberAxis)localXYPlot.getRangeAxis();
		localNumberAxis.setAutoRangeIncludesZero(false);
		localNumberAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); */
	    return localJFreeChart;
	}
	
	private XYDataset createDataset() {
		YIntervalSeries localYIntervalSeries = new YIntervalSeries("AODV");
		for(Point point : points){
			localYIntervalSeries.add(point.getX(), point.getY(), 0, 0);
		}
		YIntervalSeriesCollection localYIntervalSeriesCollection = new YIntervalSeriesCollection();
		localYIntervalSeriesCollection.addSeries(localYIntervalSeries);
		return localYIntervalSeriesCollection;
	}

	 public JPanel createPanel(){
	    JFreeChart localJFreeChart = createChart(createDataset());
	    ChartPanel localChartPanel = new ChartPanel(localJFreeChart);
	    localChartPanel.setMouseWheelEnabled(true);
	    return localChartPanel;
	  }
}
